package com.soundShape.mediators.freeConsultation
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.views.freeConsultation.FreeConsultation;
	import com.soundShape.views.login.Login;
	import com.soundShape.vo.ConsultationVO;
	import com.soundShape.vo.CustomerVO;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.core.UIComponent;
	import mx.effects.Move;
	import mx.effects.Rotate;
	import mx.events.ListEvent;
	import mx.managers.PopUpManager;
	
	import spark.components.BorderContainer;
	import spark.components.RadioButtonGroup;
	
	
	
	
	public class FreeConsultationMediator extends AbstractViewMediator
	{
		
				private var isValidConsentForm:Boolean;
				private var isValidMedHistory:Boolean;
				private var isValidText:Boolean;
				
				public function FreeConsultationMediator()
				{
					super(FreeConsultation);
				}
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():FreeConsultation
				{
					return this._view as FreeConsultation;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
					BindingUtils.bindProperty(this.view.cmbxCustomers,"dataProvider",this.applicationModel.customers,"source");
					setCustomerComboBox();
					
				}	
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					//this.view.btnMenu.addEventListener(MouseEvent.CLICK, handleBtnMenu);
					
					this.view.cmbxCustomers.addEventListener(ListEvent.CHANGE, handleCmbxCustomersChange);
					
					this.view.radGrpConsentForm.addEventListener(Event.CHANGE, handleRadGrpConsentForm);
					
					this.view.radGrpMedHistory.addEventListener(Event.CHANGE, handleRadGrpMedHistory);
					
					this.view.txtAreaLookingFor.addEventListener(KeyboardEvent.KEY_DOWN, handleTxtAreaLookingFor);
					
					this.view.btnSubmit.addEventListener(MouseEvent.CLICK, handleBtnSubmit);
				}
				
				private function setViews():void
				{
					
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnMenu(mouseEvent:MouseEvent):void
				{
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
				}
				
				private function handleCmbxCustomersChange(listEvent:ListEvent):void
				{
					this.applicationModel.customerVO = ComboBox(listEvent.currentTarget).selectedItem as CustomerVO;
				}
				
				
				
				private function handleRadGrpConsentForm(event:Event):void
				{
					isValidConsentForm = (RadioButtonGroup(event.currentTarget).selectedValue == "Yes");
					doValidation();
				}
				
				
				
				private function handleRadGrpMedHistory(event:Event):void
				{
					isValidMedHistory = (RadioButtonGroup(event.currentTarget).selectedValue == "Yes");
					doValidation();
				}
				
				
				private function handleTxtAreaLookingFor(keyboardEvent:KeyboardEvent):void
				{
					if(this.view.txtAreaLookingFor.text.length > 10)
						isValidText = true;
					else
						isValidText = false;
					
					doValidation();
				}
				
				
				private function doValidation():void
				{
					if(isValidText && isValidMedHistory && isValidConsentForm && (this.view.cmbxCustomers.selectedIndex != -1))
						this.view.btnSubmit.enabled = true;
					else
						this.view.btnSubmit.enabled = false;
				}
				
				private function handleBtnSubmit(mouseEvent:MouseEvent):void
				{
					var consultationVO:ConsultationVO = new ConsultationVO();
					consultationVO.customerID = this.applicationModel.customerVO.id;
					consultationVO.name = this.applicationModel.customerVO.name;
					consultationVO.isConsentForm = true;
					consultationVO.isMedicalHistory = true;
					consultationVO.reasons = this.view.txtAreaLookingFor.text;
					consultationVO.employeeID = this.applicationModel.employeeVO.id;
					
					this.applicationModel.entityManager.save(consultationVO);
				}
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				
				/**
				 * *******************************************************************************
				 * util
				 * *******************************************************************************
				 */
				private function setCustomerComboBox():void
				{
					if(this.applicationModel.customerVO != null)
					{
						this.view.cmbxCustomers.selectedIndex = (this.view.cmbxCustomers.dataProvider as ArrayCollection).getItemIndex(this.applicationModel.customerVO);
					}
					else
					{
						this.view.cmbxCustomers.selectedIndex = -1;
					}
				}
				
				
			
		}
		
	}
